<template>
  <div>
    <!-- 购物车
     -->
    <!-- 商品详情 -->
    <Header></Header>
    <van-sticky>
      <van-nav-bar
        title="购物车列表"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
        right-text="返回顶部"
        @click-right="onClickright"
      />
    </van-sticky>
    <div class="main">
      <div class="mian-item" v-for="item in catalist" :key="item.id">
        <div class="switch">
          <van-switch v-model="item.checked"></van-switch>
        </div>
        <div class="img">
          <van-image :src="item.thumb_path"></van-image>
        </div>
        <div class="main-right">
          <h3>{{ item.title }}</h3>
          <div class="main-right-bd">
            <span>￥{{ item.sell_price }}</span>
            <van-stepper
              v-model="item.val"
              @plus="plus(item)"
              @minus="minus(item)"
              disable-input
            ></van-stepper>
            <p @click="del(item.id)">删除</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="fd">
      <div class="fd-left">
        <h3>总计（不含运费）:{{ total }}</h3>
        <p>已经选择商品{{ sum }}件，总价￥{{ total }}元</p>
      </div>
      <div class="fd-right">
        <van-button type="danger">去结束</van-button>
      </div>
    </div>
    {{ $store.state.id }}
  </div>
</template>

<script>
import { getshopcarlist } from "../api/goods";
import { mapMutations, catadata } from "vuex";
export default {
  async created() {
    let id = [];
    JSON.parse(localStorage.getItem("catid")).forEach((item) => {
      id.push(item.id);
    });

    id = [...new Set(id)].join();
    console.log(id);

    const { data: res } = await getshopcarlist(id);

    // console.log(res);
    this.catalist = res.message;
    // console.log(this.catalist);
    this.catalist = this.catalist.map((item) => {
      let res = JSON.parse(localStorage.getItem("catid")).filter(
        (i) => i.id == item.id
      );
      return { ...item, val: res[0].val, checked: false };
    });
    console.log(this.catalist);
  },
  data() {
    return {
      checked: false,
      val: 1,
      catalist: [],
    };
  },
  methods: {
    // 返回
    onClickLeft() {
      this.$router.go(-1);
    },
    // 滚动顶部
    onClickright() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    // 拿到全局的方法 vuex
    ...mapMutations(["catadata", "addcat", "subcat"]),
    // 点击了加号
    plus(item) {
      // console.log("点击了+好");
      // 调用全局的方法
      this.addcat(item);
    },

    // 剪好
    minus(item) {
      // console.log("点击了-好");
      this.subcat(item);
    },
    // 删除
    async del(id) {
      // console.log(id);
      const confirms = await this.$dialog
        .confirm({
          message: "是否真的删除该商品",
        })
        .catch((err) => err);
      if (confirms == "confirm") {
        this.catalist = this.catalist.filter((item) => item.id !== id);
        // this.delcat(id);
        // 最新的数据保存到本地
        localStorage.setItem("catid", JSON.stringify(this.catalist));
      }

      // console.log(this.catalist);
    },
  },
  computed: {
    sum() {
      // 计算总件数
      return this.catalist.reduce((sum, i) => {
        if (i.checked) {
          sum += i.val;
        }
        return sum;
      }, 0);
    },
    // 总价
    total() {
      return this.catalist.reduce((sum, i) => {
        if (i.checked) {
          sum += i.val * i.sell_price;
        }
        return sum;
      }, 0);
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding: 5px;
  .mian-item {
    width: 100vw;
    height: 100px;
    border-bottom: 1px solid #ccc;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .switch {
      van-switch {
      }
    }

    .img {
      width: 25%;
      height: 100%;
      .van-image {
        height: 90px;
      }
    }

    .main-right {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      width: 50%;
      height: 100px;
      padding-right: 20px;
      h3 {
        word-wrap: break-word;
        word-break: normal;
        color: #52c2ff;
        font-size: 18px;
      }

      .main-right-bd {
        display: flex;
        justify-content: space-around;
        align-items: center;
        span {
          color: red;
          font-size: 18px;
        }

        van-stepper {
        }

        p {
          font-size: 14px;
        }
      }
    }
  }
}
.fd {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 50px;
  display: flex;

  justify-content: space-between;
  align-items: center;
  height: 50px;
  background-color: #eeeeee;
  .fd-left {
    h3 {
      font-weight: 700;
    }

    p {
      font-size: 12px;
      padding: 5px 0;
    }
  }

  .fd-right {
    van-button {
    }
  }
}
</style>
